import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router"

import Layout from "@/layout/index.vue"

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    component: Layout,
    redirect: "/",
    children: [
      {
        path: "",
        name: "Index",
        component: () => import("@/views/index.vue"),
      },
      {
        path: "/about",
        name: "About",
        component: () => import("@/views/about.vue"),
      },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  //跳转页面自动回到顶部
  scrollBehavior(to, from, savedPosition) {
    return new Promise((resolve) => {
      if (savedPosition) {
        return savedPosition
      } else {
        if (from.meta.saveSrollTop) {
          const top: number = document.documentElement.scrollTop || document.body.scrollTop
          resolve({ left: 0, top })
        } else {
          resolve({ left: 0, top: 0 })
        }
      }
    })
  },
})

export default router
